package com.example.last.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.last.common.Constants;
import com.example.last.common.enums.ErrorCode;
import com.example.last.common.enums.RoleEnum;
import com.example.last.dao.DoctorDao;
import com.example.last.entity.*;
import com.example.last.entity.Doctor;
import com.example.last.exception.CustomException;
import com.example.last.utils.JwtTokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.corba.se.impl.ior.OldJIDLObjectKeyTemplate;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DoctorService {
    @Resource
    private DoctorDao doctorDao;

    public Doctor login(Account account){
        Doctor dbUser = doctorDao.findByNameAndPassword(account.getUsername(),account.getPassword());
        if(ObjectUtil.isNull(dbUser))  {
            throw new CustomException(ErrorCode.USER_NOT_EXIST_ERROR);
        }
        if(!dbUser.getPassword().equals(account.getPassword())){
            throw new CustomException(ErrorCode.USER_ACCOUNT_ERROR);
        }
        //生成jwtToken令牌
        String token = JwtTokenUtils.genToken(dbUser.getId() + "-" + dbUser.getRole(),dbUser.getPassword());
        dbUser.setToken(token);
        System.out.println(token);
        return dbUser;

    }
    public void register(Account account){
        Doctor doctor =new Doctor();
        BeanUtils.copyProperties(account,doctor);
        add(doctor);
    }
    public void updatePassword(Account account) {
        Doctor dbDoctor = doctorDao.selectByUsername(account.getUsername());
        if(ObjectUtil.isNull(dbDoctor)) {
            throw new CustomException(ErrorCode.USER_NOT_EXIST_ERROR);
        }
        if(!account.getPassword().equals(dbDoctor.getPassword())) {
            throw new CustomException(ErrorCode.PARAM_PASSWORD_ERROR);
        }
        doctorDao.updatePassword(account);
    }

    public void add(Doctor doctor){
        Doctor dbDoctor = doctorDao.selectByUsername(doctor.getUsername());
        if(ObjectUtil.isNotNull(dbDoctor)){
            throw new CustomException(ErrorCode.USER_EXIST_ERROR);
        }
        if(ObjectUtil.isEmpty(doctor.getPassword())){
            doctor.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if(ObjectUtil.isEmpty(doctor.getName())){
            doctor.setName(doctor.getUsername());
        }
        if(ObjectUtil.isEmpty(doctor.getStatus())){
            doctor.setStatus("待审批");
        }
        doctor.setRole(RoleEnum.DOCTOR.name());
        doctorDao.add(doctor);

    }

    public PageInfo<Doctor> findBySearch(@Param("params") Params params){
        //开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        List<Doctor> list =  doctorDao.findBySearch(params);
        return PageInfo.of(list);
    }

    public void update(Doctor Doctor){
        doctorDao.update(Doctor);
    }

    public void delete(Integer id){
        doctorDao.delete(id);
    }
    public Doctor findById(Integer id){
        return doctorDao.selectByPrimaryKey(id);
    }


    public Doctor findByName(String username) {
        return doctorDao.findByName(username);
    }
}
